// #ifdef APP-PLUS
import config from '../../config.js'
import request from '../request.js'
import common from '../common.js'
import push from '../functions/push.js'


let app = new Object()

/**
 * 获取APP基本信息
 * 2021-02-20
 */
app.getInfo = function() {
	let device_id = uni.getStorageSync('device_id')
	let user_id = uni.getStorageSync('user_id')
	let access_token = uni.getStorageSync('access_token')

	return new Promise((resolve, reject) => {
		if (!device_id) {
			uni.getSystemInfo({
				success: (res) => {
					// 获取客户端推送标识信息
					// let client_info = plus.push.getClientInfo()
					// console.log(client_info.clientid)
					push.getPushClientId().then(res1 => {
						// console.log(res1)
						request.getData('base/app/api/User/user', {
							// 设备 id 。由 uni-app 框架生成并存储，清空 Storage 会导致改变
							device_id: res.deviceId,
							brand: res.brand,
							model: res.model,
							pixel_ratio: res.pixelRatio,
							language: res.language,
							version: res.version,
							system: res.system,
							platform: res.platform,
							client_id: res1.client_id
						}).then(res => {
							if (res.code === 0) {
								uni.setStorageSync('device_id', res
									.data.device_id)
								uni.setStorageSync('user_id', res
									.data.user_id)
								uni.setStorageSync('access_token',
									res.data.access_token)
								uni.setStorageSync('access_token_expire_time',
									res.data
									.access_token_expire_time)

								resolve({
									device_id: uni
										.getStorageSync(
											'device_id'),
									user_id: uni
										.getStorageSync(
											'user_id')
								})
								console.log(
									'生产环境刚获得: device_id = ' +
									uni.getStorageSync(
										'device_id') +
									' user_id = ' + uni
									.getStorageSync(
										'user_id') + ' access_token = ' +
									uni
									.getStorageSync('access_token'))
							} else {
								reject()
							}
						})
					})
				},
				fail: (res) => {
					reject()
				}
			});
		} else {
			resolve({
				device_id: uni.getStorageSync('device_id'),
				user_id: uni.getStorageSync('user_id')
			})
			// console.log('生产环境缓存: device_id = ' + uni.getStorageSync('device_id') + ' user_id = ' + uni
			// 	.getStorageSync('user_id') + ' access_token = ' + uni.getStorageSync('access_token'))
		}
	})
}

export default app
// #endif